package com.flink.datastreamapi.split;

import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class SplitStreamByFilter {
    public static void main(String[] args) throws Exception {
         //获取环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //设置并行度
        env.setParallelism(2);
        //获取数据源
        DataStreamSource<String> dataStreamSource = env.fromElements("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
        //数据转换 字符串的数字转存数字类型integer
        SingleOutputStreamOperator<Integer> ds = dataStreamSource.map(Integer::valueOf);//等价于 str->Integer.valueOf(str)

        //将ds 分为两个流 ，一个是奇数流，一个是偶数流
        //使用filter 过滤两次
        SingleOutputStreamOperator<Integer> ds1 = ds.filter(x -> x % 2 == 0);

        SingleOutputStreamOperator<Integer> ds2 = ds.filter(x -> x % 2 == 1);

        ds1.print("偶数");
        ds2.print("奇数");

        env.execute();
    }

}
